package Cowquisition;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

public class TilePlacement{
  private static MyJFrame f;
  static int xMin = 0, xMax = 500, yMin = 0, yMax = 300, tileHeight = 50, tileWidth = 50;
  static List<Tile> list = new ArrayList<Tile>();
  static int mapWid, mapHig;

  /**init(int, int)
    * Method used to initialize the Frame
    *@param mw - the width of the map
    *@param mh - the height of the map
    */
  public void init(int mw, int mh)
  {
    mapWid = mw;
    mapHig = mh;
    int x,y;
    f = new MyJFrame();
    f.setTitle("Drawing Graphics in Frames");
    f.setExtendedState(Frame.MAXIMIZED_BOTH);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setVisible(true);
    f.addKeyListener(null);
  }
  
  /**drawCow(int, int)
    *method used to call the drawCow() method in the MyJFrame class. Called when a cow moves
    *@param x - the x coordinate of the cow
    *@param y - the y coordinate of the cow
     */
  public void drawCow(int x, int y)
  {
    f.drawCow(x, y);
  }

  public List<Tile> getList()
  {
    return list;
  }

  /**MyJFrame
    * class that extends the JFrame class
    */
  static class MyJFrame extends JFrame implements KeyListener
  {
    private static final long serialVersionUID = 1L;
    int moveHori = 0;
    int moveVert = 0;

    //Constructor
    public MyJFrame()
    {
      this.addKeyListener(this);
    }

    /**drawCow(int, int)
      *The method used to draw the cow to the map using the paint method
      *@param x - the x coordinate of the cow
      *@param y - the y coordinate of the cow
      */
    public void drawCow(int x, int y)
    {
      //if((x > xMin && x < xMax) && (y > yMin && y < yMax))
        f.repaint();
    }
    
    /**paint(Graphics)
      *Method used to draw the tiles to the JFrame
      */
    public void paint(Graphics g)
    {
      tileHeight = 50;
      tileWidth = 50;
      ArrayList<ArrayList<Tile>> tileList = new ArrayList<ArrayList<Tile>>();
      ArrayList<ArrayList<Tile.objectType>> objectList = new ArrayList<ArrayList<Tile.objectType>>();
      int k = moveHori/tileWidth;
      for(int i = moveHori/tileWidth; i < (moveHori/tileWidth)+(this.getWidth()/tileWidth) + 1; i++){
        tileList.add(new ArrayList<Tile>());
        objectList.add(new ArrayList<Tile.objectType>());
      }
      for(int i = 0; i < (this.getWidth()/tileWidth) + 1; i ++){
        for(int j = moveVert/tileHeight; j < (moveVert/tileHeight)+(this.getHeight()/tileHeight) + 1; j++){
          tileList.get(i).add(Cowquisition.map.getTile(k, j));
          objectList.get(i).add(Cowquisition.map.getObject(k, j));
        }
        k++;
      }

      xMax = this.getWidth() + moveHori;
      yMax = this.getHeight() + moveVert;
      //System.out.println(xMin + " " + xMax + " " + yMin + " " + yMax);
      for (int i = 0; i < tileList.size(); i++){
        for(int j = 0; j < tileList.get(i).size(); j++){
          g.drawImage(tileList.get(i).get(j).getImage(), (i*tileWidth), (j*tileHeight), tileHeight, tileWidth, null);
          if(objectList.get(i).get(j) != Tile.objectType.AIR)
            g.drawImage((objectList.get(i).get(j) == Tile.objectType.TREE ? Cowquisition.tree : Cowquisition.rock), (i*tileWidth), (j*tileHeight), tileHeight, tileWidth, null);
        }
      }
    }

    /** KeyListener methods to listen for when key actions are made*/
    public void keyPressed(KeyEvent e) {
      int id = e.getKeyCode();
      //System.out.println("Key pressed: " + KeyEvent.getKeyText(id));
      if(KeyEvent.getKeyText(id).compareTo("Right")==0 && xMax<tileWidth * mapWid){
        xMax += tileWidth;
        xMin += tileWidth;
        moveHori += tileWidth;
        repaint();
      }
      if(KeyEvent.getKeyText(id).compareTo("Down")==0 && yMax<tileHeight * mapHig){
        yMax+=tileHeight;
        yMin+=tileHeight;
        moveVert +=tileHeight;
        repaint();
      }
      if(KeyEvent.getKeyText(id).compareTo("Left")==0 && xMin!= 0){
        xMax -= tileWidth;
        xMin -= tileWidth;
        moveHori-=tileWidth;
        repaint();
      }
      if(KeyEvent.getKeyText(id).compareTo("Up")==0 && yMin!=0){
        yMax-=tileHeight;
        yMin-=tileHeight;
        moveVert -=tileHeight;
        repaint();
      }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
     
    }
  }
}
